//
//  LHPictureDetailControllerViewController.m
//  Project_B
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "LHPictureDetailController.h"

@interface LHPictureDetailController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic)NSMutableArray *dataArray;
@property (strong, nonatomic)UICollectionView *collection;
@end

@implementation LHPictureDetailController

- (void)viewWillAppear:(BOOL)animated
{  
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self createCollection];
    [self getData];
}

#pragma mark ** 创建collectionView
- (void)createCollection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:layout];
    
    
    //横向滑动
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //大小
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT-64);
    //行间距
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    //取消滑动反弹
    _collection.bounces = NO;
    //按页滑动
    _collection.pagingEnabled = YES;
    
    _collection.backgroundColor = [UIColor blackColor];
    [_collection registerClass:[LHPictureDetailCell class] forCellWithReuseIdentifier:@"cell"];
    _collection.delegate = self;
    _collection.dataSource = self;
   
    [self.view addSubview:_collection];
}

#pragma mark ** 数据请求
- (void)getData {
    self.dataArray = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat:@"http://c.m.163.com/photo/api/set/0096/%@.json", self.setid];
    [LHAFNTools appToolsWithSourcePath:url block:^(id result) {
        NSInteger i = 0;

        NSMutableArray *array = [result objectForKey:@"photos"];
        
        for (NSMutableDictionary *dic in array) {
            LHPictureDetailModel *model = [[LHPictureDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
            model.labelH = [LhHeightForContent heightForContent:[NSString stringWithFormat:@"%@, %@",model.imgtitle, model.note] font:14.5 width:SCREEN_WIDTH];
            model.setname = _setname;
            i++;
            model.currentPage = i;
        }
        [_collection reloadData];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"%ld",_dataArray.count);
    return _dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LHPictureDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = _dataArray[indexPath.item];
    NSString *title = [NSString stringWithFormat:@"%ld/%ld",[_dataArray[indexPath.item] currentPage], _dataArray.count];
    NSString *str = [NSString stringWithFormat:@"/%ld", _dataArray.count]; //是需要变小的文字
    // 设置标签文字
    NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:title];
    
    // 获取标红的位置和长度
    NSRange range = [title rangeOfString:str]; //获取需要变量文字的文字
    
    // 设置标签文字的属性
    [attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:14.0]} range:range];
    
    // 显示在Label上
    cell.pageLabel.attributedText = attrituteString;
    return cell;
}



//设置缩放哪个view
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
     NSInteger page = self.collection.contentOffset.x / self.collection.frame.size.width;
    NSLog(@"缩放%ldView", page);
    NSLog(@"%ld",_collection.subviews.count);
    return [_collection.subviews objectAtIndex:page];
}

//缩放时一直触发的方法
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
     NSInteger page = self.collection.contentOffset.x / self.collection.frame.size.width;
    UIView *a = [[UIView alloc] init];
    a =  [scrollView.subviews objectAtIndex:page];
    a.center = CGPointMake(SCREEN_WIDTH/2 + page * SCREEN_WIDTH, SCREEN_HEIGHT/2);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
